package com.example.app;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class NewsListActivity extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabPagerAdapter adapter;
    private ActionBar actionBar;
    private int defaultTab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_noticias);
        setTitle("Lista de Notícias");
        Bundle extras = getIntent().getExtras();
        this.defaultTab = extras.getInt("TAB");
        getOverflowMenu();

        actionBar = getActionBar();
        viewPager = (ViewPager)findViewById(R.id.pager);
        adapter = new TabPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setHomeButtonEnabled(false);

        actionBar.addTab(actionBar.newTab().setText(R.string.moba).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.rts).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.fps).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.fight).setTabListener(this));

        actionBar.setSelectedNavigationItem(defaultTab);
        viewPager.setCurrentItem(defaultTab,false);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                actionBar.setSelectedNavigationItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void getOverflowMenu() {

        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if(menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    };

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.lista_noticias, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        Intent intent = new Intent(NewsListActivity.this, novoAqui.class);
        Bundle extras = new Bundle();

        switch (item.getItemId()) {
            case R.id.esports_menu:
                extras.putInt("helper", 0);
                intent.putExtras(extras);
                startActivity(intent);
                break;

            case R.id.moba_menu:
                extras.putInt("helper", 1);
                intent.putExtras(extras);
                startActivity(intent);
                break;

            case R.id.rts_menu:
                extras.putInt("helper", 2);
                intent.putExtras(extras);
                startActivity(intent);
                break;

            case R.id.fps_menu:
                extras.putInt("helper", 3);
                intent.putExtras(extras);
                startActivity(intent);
                break;

            case R.id.fight_menu:
                extras.putInt("helper", 4);
                intent.putExtras(extras);
                startActivity(intent);
                break;

            case R.id.sync_btn:
                Toast.makeText(getApplicationContext(),"Notícias atualizadas!",Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onBackPressed(){
        super.onBackPressed();
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
    }
}